<?php
App::uses('AppController', 'Controller');
/**
 * OptionTypes Controller
 *
 * @property OptionType $OptionType
 */
class OptionTypesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->OptionType->recursive = 0;
		$this->set('optionTypes', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->OptionType->id = $id;
		if (!$this->OptionType->exists()) {
			throw new NotFoundException(__('Invalid option type'));
		}
		$this->set('optionType', $this->OptionType->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->OptionType->create();
			if ($this->OptionType->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The option type has been saved'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The option type could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->OptionType->id = $id;
		if (!$this->OptionType->exists()) {
			throw new NotFoundException(__('Invalid option type'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->loadModel('OptionValue');
			$optionType = $this->OptionType->read(null, $id);
			if ($this->OptionType->saveAssociated($this->request->data)) {
				foreach($optionType['OptionValues'] as $value){
					$flag = false;
					foreach($this->request->data['OptionValues'] as $option	){
						if((!empty($option['id']) || isset($option['id'])) && $option['id'] == $value['id']){
							$flag = true;
						}
					}
					if($flag == false){
						$this->OptionValue->id = $value['id'];
						if(!$this->OptionValue->delete()) {
							//throw message when can't delete option value
						}
					}
				}
				$this->Session->setFlash(__('The option type has been saved'), 'default', array(), 'good');
				$this->request->data = $this->OptionType->read(null, $id);
				#$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The option type could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
			$this->request->data = $this->OptionType->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->OptionType->id = $id;
		if (!$this->OptionType->exists()) {
			throw new NotFoundException(__('Invalid option type'));
		}
		if ($this->OptionType->delete()) {
			$this->Session->setFlash(__('Option type deleted'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Option type was not deleted'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}

/**
 * update status
 * @throws NotFoundException
 * @param array $ids
 * @return void
 */
	public function admin_updateStatus(){
		if(!in_array($this->request->data['OptionType']['status'], array(0,1))){
			throw new NotFoundException(__('Invalid Option Type Status'));
		}
		if($this->OptionType->updateAll(array('status' => $this->request->data['OptionType']['status']),
																 array('id' => $this->request->data['ids']))){
			$this->Session->setFlash(__('Option types have been updated status'), 'default', array(), 'good');
		}else{
			$this->Session->setFlash(__('Can not update Option types! Please try again.'), 'default', array(), 'bad');
		}
		$this->redirect(array('action' => 'index'));
	}
}
